import numpy as np
import matplotlib.pyplot as plt


x_train = np.array([3.3, 4.4, 5.5, 6.71, 6.93, 4.168,
                    9.779, 6.182, 7.59, 2.167, 7.042,
                    10.791, 5.313, 7.997, 3.1], dtype=np.float32)
y_train = np.array([1.7, 2.76, 2.09, 3.19, 1.694, 1.537,
                    3.366, 2.596, 2.53, 1.221, 2.827,
                    3.465, 1.65, 2.904, 1.3], dtype=np.float32)
x_train = np.expand_dims(x_train, axis=1)
y_train = np.expand_dims(y_train, axis=1)
num_samples = len(x_train)
plt.scatter(x_train, y_train, alpha=0.5, edgecolors='white')
# plt.show()

X = np.concatenate((x_train, np.ones_like(x_train)), axis=1)
Y = y_train
print(X.shape)
print(Y.shape)
w_ = np.linalg.inv(np.dot(X.T, X))
w_ = np.dot(w_, X.T)
w_ = np.dot(w_, Y)
w_ = np.squeeze(w_)

y_pred = w_[0] * x_train + w_[1]
plt.plot(x_train, y_pred)

se = 0
for i in range(num_samples):
    se += np.square(np.squeeze(y_train[i]) - np.squeeze(y_pred[i]))
se /= num_samples
print('Matrix solver loss: {}'.format(se))

plt.show()
